import subprocess
import os
import time

zm_list = "zm_list.txt"

ytdlp_path = "D:\\tool\\video\\yt-dlp.exe" # 提供 yt-dlp 的完整路径
PROXY = "http://127.0.0.1:10809"
COOKIES_FILE = r"D:\Downloads\www.youtube.com_cookies.txt"
VIDEO_URL = "https://www.youtube.com/watch?v=GWGbOjlJDkU"

language_txt = 'Chinese (Traditional)'

# 定义命令和参数
command_list = [
    ytdlp_path,
    "--list-subs",
    "--proxy", PROXY,  # 使用配置的代理
    "--cookies", COOKIES_FILE,  # 使用配置的 cookies 文件
    VIDEO_URL  # 使用配置的视频链接
]


# 执行命令并捕获输出
result = subprocess.run(command_list, capture_output=True, text=True)

# 将输出保存到文件

with open(zm_list, "w", encoding="utf-8") as file:
    if result.returncode == 0:
        file.write("命令输出：\n")
        file.write(result.stdout)
    else:
        file.write("命令执行出错：\n")
        file.write(result.stderr)

print(f"命令执行结果已保存到 {zm_list}")

# 先看视频链接翻译的哪个字幕最合适,然后在字幕列表输出内容里找相应文本,选择所需字幕

# 遍历每一行，找出包含所需文本的行
with open(zm_list, 'r', encoding='utf-8') as file:
    lines = file.readlines()

matching_lines = [line for line in lines if language_txt in line]
for line in matching_lines:
    print(line)


# # 如果你使用原来的插件,运行以下代码可以自动在一分钟后删除之前下载的cookies文件

# # 删除cookies文件
# print("代码暂停 60 秒...")
# time.sleep(60)
# print("暂停结束，继续执行代码。")

# os.remove(COOKIES_FILE)
# print("已删除cookies文件。")